package com.sikanrong;
// External imports
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import com.sun.opengl.util.Animator;
import javax.media.opengl.GL;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;


public class RubyGLApplet extends Applet
{
	GLCapabilities caps;
	GLCanvas canvas;
	StringBuffer buf; 
	
    public RubyGLApplet()
    {

        setLayout(new BorderLayout());

        setSize(400, 400);

        setVisible(true);
	    caps = new GLCapabilities();
        caps.setDoubleBuffered(true);
        caps.setHardwareAccelerated(true);
	    canvas = new GLCanvas(caps);

        add(canvas, BorderLayout.CENTER);

        Animator anim = new Animator(canvas);
        anim.start();
    }
	public void init(){
		
		
		RubyGLWrapper ruby = RubyGLWrapper.getInterfaceFromScript(readFileFromURL(getParameter("evalruby")));

		GLEventListener listen = ruby.getGLListener();

        canvas.addGLEventListener(listen);

	}

	public String readFileFromURL(String strUrl){
	    String line;
	    URL url = null;
	    try{
	      url = new URL(strUrl);
	    }
	    catch(MalformedURLException e){}
		StringBuffer strBuff = null;
	    try{
	      InputStream in = url.openStream();
	      BufferedReader bf = new BufferedReader(new InputStreamReader(in));
	      strBuff = new StringBuffer();
	      while((line = bf.readLine()) != null){
	        strBuff.append(line + "\n");
	      }

	    }
	    catch(IOException e){
	      e.printStackTrace();
	    }
		return new String(strBuff);
	}


}
